from sqlalchemy.orm import Session

from app.db import base  # noqa: F401
from app.core.config import settings
from app.core.password import get_password_hash
from ..database import Base, engine
from ..models.user import User

def init_db(db: Session) -> None:
    # 创建所有表
    Base.metadata.create_all(bind=engine)

    # 创建数据库会话
    try:
        # 创建超级管理员用户
        user = db.query(User).filter(User.username == "admin").first()
        if not user:
            user = User(
                username="admin",
                email="admin@example.com",
                hashed_password=get_password_hash("admin"),
                full_name="Administrator",
                is_active=True,
                is_superuser=True,
            )
            db.add(user)
            db.commit()
            db.refresh(user)
    finally:
        db.close() 